﻿using Microsoft.WindowsAPICodePack.ApplicationServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFRestartRecovery
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
           RegisterForRestartRecovery();
            //... ...
        }

        private void closeBtn_Click(object sender, RoutedEventArgs e)
        {
            UnRegisterRestartRecovery();
            App.Current.Shutdown();
        }

        /// <summary>
        ///  在RegisterForRestartRecovery 方法中要分别创建Restart 和Recovery 设置（Settings）。
        ///  在RestartSettings 中可以设置命令行（“restart”），以及Restart 限制条件。
        ///  在本例中如果应用程序崩溃是因为PC 重启或安装系统补丁则不会发生Restart 功能。
        ///  最后要通过ApplicationRestartRecoveryManager 类将Restart 和Recovery 设置分别注册。
        /// </summary>
        private void RegisterForRestartRecovery()
        {
            RestartSettings restartSettings = new RestartSettings("restart",
                RestartRestrictions.NotOnReboot | RestartRestrictions.NotOnPatch);
            ApplicationRestartRecoveryManager.RegisterForApplicationRestart(restartSettings);

            RecoveryData data = new RecoveryData(new RecoveryCallback(PerformRecovery), null);
            RecoverySettings recoverySettings = new RecoverySettings(data, 0);
            ApplicationRestartRecoveryManager.RegisterForApplicationRecovery(recoverySettings);
        }

        /// <summary>
        /// 注销方式使用UnregisterApplicationRestar和 UnregisterApplicationRecovery 两种方法即可。
        /// </summary>
        private void UnRegisterRestartRecovery()
        {
            ApplicationRestartRecoveryManager.UnregisterApplicationRestart();
            ApplicationRestartRecoveryManager.UnregisterApplicationRecovery();
        }


        /// <summary>
        /// 在应用程序恢复过程中还需要编写一个恢复过程，即RegisterForRestartRecovery 方法提到的PerformRecovery。首先可以通过ApplicationRecoveryInProgress 方法判断恢复过程是否在进行。
        /// 如果恢复过程被用户取消了，则可以将应用程序进程杀掉，并通过ApplicationRecoveryFinished 方法设置恢复过程是否完成。
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        private int PerformRecovery(object state)
        {
            bool isCanceled = ApplicationRestartRecoveryManager.ApplicationRecoveryInProgress();
            if (isCanceled)
            {
                ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(false);
            }

            //recovery your work here ...

            ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(true);
            return 0;
        }

        private void crashBtn_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
